package com.leen.news.feign.filter;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.plugin.liveconnect.SecurityContextHelper;

import javax.servlet.http.HttpServletRequest;

/**
 * feign拦截令牌，并且传递给下一个调用方法
 *
 * @author admin
 * @date 2021-05-20
 */
public class FeignTokenInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {

        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();


        HttpServletRequest request = attributes.getRequest();


        //只需token
        String token=request.getHeader("X-Token");
        if(StringUtils.isNotBlank(token)){
            //转移
            requestTemplate.header("X-Token", token);
        }
    }
}
